package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuImage;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ author     ：廖传港
 * @ date       ：Created in 2020/12/24 16:40
 * @ description：
 * @ modified By：
 * @ ersion:
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private SkuInfoMapper skuInfoMapper;

    @Autowired
    private SkuImageMapper skuImageMapper;

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    private SkuSaleAttrValueMapper skuSaleAttrValueMapper;

    //注入redis
    @Autowired
    private RedisTemplate redisTemplate;

    // 查询三级分类
    @Override
    @GmallCache(prefix = "getBaseCategoryView")//自定义缓存的注解
    public BaseCategoryView getBaseCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    //查询SkuInfo对象,获取sku基本信息与图片信息
    @Override
    @GmallCache(prefix = "getSkuInfo")  //自定义缓存的注解
    public SkuInfo getSkuInfo(Long skuId) {

        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);
            //判断是否NULL
            if (null != skuInfo) {
                List<SkuImage> skuImageList = skuImageMapper.
                        selectList(new QueryWrapper<SkuImage>().eq("sku_id", skuId));
                skuInfo.setSkuImageList(skuImageList);
            }
        return skuInfo;
    }


    //查询价格
    @Override
    public BigDecimal getSkuPrice(Long skuId) {
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);
        if (null != skuInfo) {
            return skuInfo.getPrice();
        }
        return null;
    }

    //商品详情之查询销售属性及选中
    @Override
    @GmallCache(prefix = "getSpuSaleAttrAndChecked")//自定义缓存的注解
    public List<SpuSaleAttr> getSpuSaleAttrAndChecked(Long spuId, Long skuId) {
        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrMapper.getSpuSaleAttrAndChecked(spuId,skuId);
        return spuSaleAttrList;
    }

    //通过SPU查询基本属性值
    @Override
    @GmallCache(prefix = "getSaleAttrValuesBySpu")//自定义缓存的注解
    public Map getSaleAttrValuesBySpu(Long spuId) {
        HashMap result = new HashMap<>();
        List<Map> saleAttrValuesBySpulist = skuSaleAttrValueMapper.getSaleAttrValuesBySpu(spuId);
        saleAttrValuesBySpulist.forEach(map -> {
            result.put(map.get("value_id"), map.get("sku_id"));
        });
        return result;
    }

    //获取首页的分类视图集合
    @Override
    public List<BaseCategoryView> getBaseCategoryViewList() {
        return baseCategoryViewMapper.selectList(null);
    }
}
